package com.kyzh.core.activities

import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.google.common.eventbus.EventBus
import com.gushenge.core.base.activity.BaseActivity
import com.gushenge.core.base.fragment.BaseFragment
import com.gushenge.core.beans.UserInfo
import com.gushenge.core.impls.GameImpl
import com.gushenge.core.impls.UserImpl
import com.gushenge.core.listeners.ResultListener
import com.gushenge.core.toast
import com.kyzh.core.R
import com.kyzh.core.adapters.FragmentPagerAdapter
import com.kyzh.core.fragment.BackgroundFragment
import com.kyzh.core.popup.CDKPopupWindow
import com.kyzh.core.utils.loadImage
import com.lxj.xpopup.XPopup
import kotlinx.android.synthetic.main.act_background.*
import kotlinx.android.synthetic.main.act_background.titleView
import kotlinx.android.synthetic.main.act_game_background.*
import kotlinx.android.synthetic.main.activity_gift.*
import kotlinx.android.synthetic.main.activity_gift.tabLayout
import kotlinx.android.synthetic.main.activity_vipcentre.*
import kotlinx.android.synthetic.main.frag_home_title.*
import per.goweii.layer.core.utils.Utils.requireActivity


// 后台
class BackgroundActivity : BaseActivity() {

    private var viewPagerAdapter: FragmentPagerAdapter<BaseFragment>? = null

    companion object {
        fun start(context: Context) {
            val intent = Intent(context, BackgroundActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.act_background)
        initView()
        initData()

    }

    private fun initView() {
        setTitle("游戏管理")
        viewPagerAdapter = FragmentPagerAdapter<BaseFragment>(this).apply {
            addFragment(BackgroundFragment.getInstance(1), "普通包站")
            addFragment(BackgroundFragment.getInstance(2), "稀有包站")
            viewPagerBackGround.adapter = this
            tabLayoutBackGround.setupWithViewPager(viewPagerBackGround)
        }
        tvPtQx.setOnClickListener {
            showPopupDialog("1")
        }
        tvXyQx.setOnClickListener {
            showPopupDialog("2")
        }
    }

    private fun initData() {
        refreshNumber()
    }

    private fun showPopupDialog(type: String) {
        XPopup.Builder(this)
            .asCustom(CDKPopupWindow(this) {
                submitCode(type, it.toString())
            })
            .show()
    }

    private fun submitCode(str: String, code: String) {
        GameImpl.submitCode(str, code) {
            if (this.code == 1) {
                refreshNumber()
            }
            this.msg.toast()
        }
    }

    // 获取包站次数
    fun refreshNumber() {
        UserImpl.userInfo(object : ResultListener {
            override fun success(user: Any) {
                val item = user as UserInfo
                tvPtCs.text = "普通包站会员重绑次数：${item.gm_unbind_num}"
                tvXyCs.text = "稀有包站会员重绑次数：${item.xiyou_unbind_num}"
            }

            override fun error(error: String) {
                icon.setImageResource(R.drawable.logo_new)
            }
        })
    }
}
